package com.harry.web.tag;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class Menu extends BodyTagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
 
    
    
	public void setBodyContent(BodyContent bc){
		super.setBodyContent(bc);		
	}
	
	
	private int width;
	
	private String dataSource;
	
	private String iconPath;
	
	private String colIdName;
	
	private String colParentIdName;
	
	private String colTitleName;
	
	private String colSrcName;
	
	private String colIconName;
	

	public void setWidth(int width) {
		this.width = width;
	}

	public void setDataSource(String dataSource) {
		this.dataSource = dataSource;
	}
	
	public void setIconPath(String iconPath){
		this.iconPath = iconPath;
	}
	
	public void setColIdName(String colIdName) {
		this.colIdName = getMethodName(colIdName);
	}
	public void setColParentIdName(String colParentIdName) {
		this.colParentIdName = getMethodName(colParentIdName);
	}
	public void setColTitleName(String colTitleName) {
		this.colTitleName = getMethodName(colTitleName);
	}
	public void setColSrcName(String colSrcName) {
		this.colSrcName = getMethodName(colSrcName);
	}
	
	public void setColIconName(String colIconName) {
		this.colIconName = getMethodName(colIconName);
	}
	
	private String getMethodName(String propertyName){
		return "get"+propertyName.substring(0, 1).toUpperCase()+propertyName.substring(1);
	}
	private String path="";
	
	@SuppressWarnings("unchecked")
	private Collection collection=null;
	
	@SuppressWarnings("unchecked")
	public int doAfterBody(){
		try{
			path=pageContext.getServletContext().getContextPath();
			BodyContent bc = super.getBodyContent();
			Map<Integer,Collection> maps = (Map<Integer,Collection>)pageContext.getAttribute(dataSource, PageContext.APPLICATION_SCOPE);
			
			collection = maps.get(new Integer(bc.getString()));

			JspWriter out = bc.getEnclosingWriter();
			out.println("<style type=\"text/css\">");
			out.println("*{font:11px tahoma,arial,sans-serif;}");
			out.println("ul{margin:0px;padding:0px;border:0px;}");
			out.println("#nav{width:"+width+"px;margin: 0px;padding: 0px;font-size: 14px;line-height: 30px;}");
			out.println("#nav li{margin: 0px;padding: 0px;margin-bottom:10px;width: 100%;border:solid 1px #DFDCD2;list-style-image: none;list-style-type: none;background-image:url("+path+"/images/mk_r4_c4.gif);}");
		    out.println("#nav a{margin: 0px;padding-left: 10px;cursor:hand;display: block;height:37px;line-height:37px;vertical-align:middle;text-align:right;text-decoration: none;}");
            out.println("#nav a div{height:37px;line-height:37px;vertical-align:middle;text-decoration: none;}");
		    out.println("#nav a:hover{color:#ff0000;text-decoration:underline;}");
		    out.println("#nav li ul{padding-top: 1px;list-style-image: none;list-style-type: none;}");
		    out.println("#nav li li{height:30px;line-height:30px;background-image:url();background-color: #FFF2FC;border-width:0px;margin-bottom:0px;padding-left: 0px;}");
		    out.println("#nav li li li{height:30px;line-height:30px;background-image:url();background-color: #FFF2FC;border-width:0px;margin-bottom:0px;padding-left: 20px;}");
		    out.println("#nav ul a{text-align:left;cursor:hand;}");
		    out.println(".cx {display:none;visibility:hidden;}");
		    out.println(".ex {display:inherit;visibility:inherit;}");
		    out.println("</style>");
			out.println("<script language=\"javascript\" type=\"text/javascript\">");
			
			
			out.println("function doMenu(obj,strDeep){");
			out.println("var items=obj.parentNode.getElementsByTagName(\"ul\");");
			out.println("var itmUl;var deeps=strDeep;");
			out.println("if(items.length>0){");
			out.println("itmUl=items[0];");
			out.println("}");
			out.println("if(itmUl.className!=\"ex\"){");
			out.println("cxAll();");
			out.println("if(deeps=='1'){");
			out.println("itmUl.parentNode.style.backgroundImage='url("+path+"/images/mk_r9_c4.gif)';");
			out.println("itmUl.parentNode.style.borderColor='#FFcece';");
			out.println("}");
			out.println("else if(deeps=='2'){");
			out.println("itmUl.parentNode.parentNode.parentNode.style.backgroundImage='url("+path+"/images/mk_r9_c4.gif)';");
			out.println("itmUl.parentNode.parentNode.parentNode.style.borderColor='#FFcece';");
			out.println("itmUl.parentNode.parentNode.className=\"ex\";");
			out.println("itmUl.parentNode.getElementsByTagName(\"img\")[0].src=\""+path+"images/down.png\";");
			out.println("}");
			out.println("itmUl.className=\"ex\";");
			out.println("}");
			out.println("else{");
			out.println("if(deeps=='1'){");
			out.println("itmUl.parentNode.style.backgroundImage='url("+path+"/images/mk_r4_c4.gif)';");
			out.println("itmUl.parentNode.style.borderColor='#DFDCD2';");
			out.println("}");
			out.println("else if(deeps=='2'){");  
			out.println("itmUl.parentNode.getElementsByTagName(\"img\")[0].src=\""+path+"/images/up.png\";");
			out.println("}");
			out.println("itmUl.className=\"cx\";");
			out.println("}");
			out.println("}");

			
			out.println("function cxAll(){");
			out.println("var ulDom=document.getElementById(\"nav\");");
			out.println("var items=ulDom.getElementsByTagName(\"ul\");");
			out.println("for (var i=0;i<items.length;i++){");
			out.println("if(items[i].parentNode.style.backgroundImage=='url("+path+"/images/mk_r9_c4.gif)'){");
			out.println("items[i].parentNode.style.borderColor='#DFDCD2';");
			out.println("items[i].parentNode.style.backgroundImage='url("+path+"/images/mk_r4_c4.gif)';");
			out.println("}");
			out.println("var arImg = items[i].getElementsByTagName(\"img\");");
			out.println("for (var k=0;k<arImg.length;k++){");
			out.println("var sImg = arImg[k].src;");
			out.println("if (sImg.indexOf(\".png\")>0){");
			out.println("if (sImg.indexOf(\"images/down.png\")>0){");
			out.println("arImg[k].src=sImg.replace(\"images/down.png\",\"images/up.png\");");
			out.println("}"); 
			out.println("}");
			out.println("}");
			out.println("items[i].className=\"cx\";");
			out.println("}");
			out.println("}");
			
			out.println("function startUp(){");
			out.println("cxAll();");
			out.println("var ulDom=document.getElementById(\"nav\");");
			out.println("var items=ulDom.getElementsByTagName(\"ul\");");
			out.println("}");
			
			
			out.println("window.onload=function(){");
			out.println("startUp();");
			out.println("}");
			
		    out.println("</script>");
		    
			out.println("<ul id=\"nav\">");
			//System.out.println(this.colIdName+":"+this.colParentIdName+":"+this.colTitleName+":"+this.colSrcName);
			Iterator ite = collection.iterator();
			while(ite.hasNext()){
				Object o=ite.next();
				if(execute(o,colParentIdName).equals(0)){
					//System.out.println("Main:"+o.toString());
					out.println("<li><a onclick=\"doMenu(this,'1')\"><div style=\"text-align:left;float:left;padding-top:12px;\"><img alt=\"task\" src=\""+path+iconPath+execute(o,colIconName)+"\"/></div><div style=\"font-weight:bold;\">"+execute(o,colTitleName)+"</div></a>");
					out.println("<ul>");
					out.println(" <li style=\"height:1px;overflow-y:hidden;\"><div align=\"center\" style=\"background-color:#fff2fc;\"><div style=\"height:1px; width:95%;border-top:solid 1px #dddddd;overflow-y:hidden;\"></div></div></li>");
					out.println(createSecond(execute(o,colIdName),collection.iterator()));
					out.println("</ul>");
					out.println("</li>");
				}
			}
			out.println("</ul>");
		}catch(SecurityException e){
			
		}catch(NoSuchMethodException e){
			
		}catch(IllegalArgumentException e){
			
		}catch(InvocationTargetException e){
			
		}catch(IllegalAccessException e){
			
		}catch(NullPointerException e){
		}catch(IOException e){
			
		}
			
		return EVAL_PAGE;
	}
	
	private Object execute(Object obj,String name) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		return obj.getClass().getMethod(name).invoke(obj);
	}
	@SuppressWarnings("unchecked")
	private String createSecond(Object upid,Iterator iterator) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
		String code="";
		while(iterator.hasNext()){
			Object o=iterator.next();
			if(execute(o,colParentIdName).equals(upid)){
				//System.out.println("createSecond:"+o.toString());
				if(!isLeaf(execute(o,colIdName),collection.iterator())){
					//System.out.println("NotLeaf");
					code+="<li><a onclick=\"doMenu(this,'2')\"><div style=\"text-align:left;float:left;padding-top:11px;\"><img alt=\"g\" src=\""+path+iconPath+execute(o,colIconName)+"\"/><img alt=\"g\" src=\""+path+"/images/spacer.gif\" width=\"5\"/></div><div>"+execute(o,colTitleName)+"</div></a>";
					code+="<ul>";
					code+=createThird(execute(o,colIdName),collection.iterator());
					code+="</ul>";
					code+="</li>";
				}else{
					//System.out.println("IsLeaf");
					code+="<li><a href=\""+path+execute(o,colSrcName)+"\"><div style=\"text-align:left;float:left;padding-top:11px;\"><img alt=\"g\" src=\""+path+iconPath+execute(o,colIconName)+"\"/><img alt=\"g\" src=\""+path+"/images/spacer.gif\" width=\"5\"/></div><div>"+execute(o,colTitleName)+"</div></a></li>";
				}
			}
		}
		return code;
	}
	@SuppressWarnings("unchecked")
	private String createThird(Object upid,Iterator iterator) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
		String code="";
		while(iterator.hasNext()){
			Object o=iterator.next();
			if(execute(o,colParentIdName).equals(upid)){
				//System.out.println("createThird:"+o.toString());
				code+="<li><a href=\""+path+execute(o,colSrcName)+"\"><div style=\"text-align:left;float:left;padding-top:11px;\"><img alt=\"g\" src=\""+path+iconPath+execute(o,colIconName)+"\"/><img alt=\"g\" src=\""+path+"/images/spacer.gif\" width=\"5\"/></div><div>"+execute(o,colTitleName)+"</div></a></li>";
			}
		}
		return code;
	}
	@SuppressWarnings("unchecked")
	private boolean isLeaf(Object id,Iterator iterator) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
	{
		while(iterator.hasNext()){
			Object o=iterator.next();
			//System.out.println("isLeaf:"+o.toString());
			if(execute(o,colParentIdName).equals(id)){
				return false;
			}
		}
		return true;
	}
}


